/*
 * Copyright Thoughtworks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import m from "mithril";
import Stream from "mithril/stream";
import { i18n } from "helpers/i18n";
import * as Buttons from "views/components/buttons";
import { Modal, Size } from "views/components/modal";
import { OperationState } from "views/pages/page_operations";

export class ToggleConfirmModal extends Modal {
  private readonly message: m.Children;
  private onsave: () => Promise<any>;
  private ajaxOperationMonitor = Stream<OperationState>(OperationState.UNKNOWN);

  constructor(message: m.Children, onsave: () => Promise<any>) {
    super(Size.small);
    this.message = message;
    this.onsave = onsave;
  }

  body(): m.Children {
    return <div>{this.message}</div>;
  }

  title(): string {
    return i18n.tSyncForPage("admin.maintenance.confirm.title", "admin-maintenance");
  }

  buttons(): m.ChildArray {
    return [
      <Buttons.Primary
        data-test-id="save-pipeline-group"
        ajaxOperationMonitor={this.ajaxOperationMonitor}
        ajaxOperation={this.onsave.bind(this)}
      >
        {i18n.tSyncForPage("admin.maintenance.confirm.save", "admin-maintenance")}
      </Buttons.Primary>,
      <Buttons.Cancel
        data-test-id="cancel-button"
        ajaxOperationMonitor={this.ajaxOperationMonitor}
        onclick={this.close.bind(this)}
      >
        {i18n.tSyncForPage("admin.maintenance.confirm.cancel", "admin-maintenance")}
      </Buttons.Cancel>,
    ];
  }
}
